﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainForm.cs" company="HelloWorld">
//   BY田新宇
// </copyright>
// <summary>
//  包括成绩单的显示和增删改查方法
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Form
{
    using System;
    using System.Data;
    using System.Windows.Forms;

    using WindowsFormsApp2.Service;

    /// <summary>
    /// The mainForm.
    /// </summary>
    public partial class Mainform : Form
    {
        /// <summary>
        /// The auto adapt size.
        /// </summary>
        private readonly AutoAdaptWindowsSize autoAdaptSize;

        /// <summary>
        /// Initializes a new instance of the <see cref="Mainform"/> class.
        /// </summary>
        public Mainform()
        {
            this.InitializeComponent();
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.autoAdaptSize = new AutoAdaptWindowsSize(this);
            this.autoAdaptSize.InitControlsInfo(this.Controls[0]);
        }
        
        /// <summary>
        /// The initial score.
        /// </summary>
        public void InitialScore()
        {
            this.dataGridView1.Visible = true;
            var db = new LinkSqLiteDB();
            var ds = new DataSet();
            if (db.DBConn())
            {
                // string sql = "select * from score_info where stuid='" + textBox1.Text + "' and stuname ='" + textBox3.Text + "'";
                var sql = "select * from score_info where stuid=" + LoginForm.StudentId;
                ds = db.GetScoreDs(sql);
                this.dataGridView1.DataSource = ds.Tables[0];
                this.dataGridView1.Update();
                dataGridView1.Columns[8].Visible = false;  
            }

            db.DBClose();

            if (ds.Tables[0].Rows.Count > 0)
            {
                // 获取学分
                var dt = ds.Tables[0];
                double jd = 0;

                // int sum_score = 0;
                double sumCredit = 0;
                double sumSc = 0; // 学分绩

                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var score = Convert.ToInt32(dt.Rows[i]["grade"].ToString());
                    var credit = Convert.ToInt32(dt.Rows[i]["credit"]);

                    if (score >= 90 && score <= 100)
                    {
                        jd = 4;
                    }

                    if (score >= 85 && score <= 89)
                    {
                        jd = 3.7;
                    }

                    if (score >= 82 && score <= 84)
                    {
                        jd = 3.3;
                    }

                    if (score >= 78 && score <= 81)
                    {
                        jd = 3.0;
                    }

                    if (score >= 75 && score <= 77)
                    {
                        jd = 2.7;
                    }

                    if (score >= 72 && score <= 74)
                    {
                        jd = 2.3;
                    }

                    if (score >= 68 && score <= 71)
                    {
                        jd = 2.0;
                    }

                    if (score >= 64 && score <= 67)
                    {
                        jd = 1.5;
                    }

                    if (score >= 60 && score <= 63)
                    {
                        jd = 1.0;
                    }

                    if (score < 60)
                    {
                        jd = 0;
                    }

                    sumSc += credit * jd;
                    sumCredit += credit;
                }

                this.label2.Text = @"平均学分绩点为：" + Math.Round(sumSc / sumCredit, 2);

                // MessageBox.Show(sum_credit.ToString());
            }
            else
            {
                this.label2.Text = @"平均学分绩点为：0";
            }
        }

        /* 
         public double gpa(stuid)
         {

         }*/

        /// <summary>
        /// The delete score.
        /// </summary>
        /// <param name="stuid">
        /// The stuId.
        /// </param>
        /// <param name="numofcourse">
        /// The numberOfCourse.
        /// </param>
        public void DeleteScore(string stuid, string numofcourse)
        {
            var db = new LinkSqLiteDB();   // 创建数据库连接对象
            if (db.DBConn())
            {
                var str = "DELETE FROM score_info WHERE stuid='" + stuid + "' and numofcourse='" + numofcourse + "'";
                const string Info = "删除成功！";

                if (db.UpdateDeleteAdd(str))
                {
                    MessageBox.Show(Info, @"标题", MessageBoxButtons.OK, MessageBoxIcon.Question);

                    // if (dr == DialogResult.OK)
                    // {
                    // //this.Visible = false;//隐藏当前窗口
                    // }
                    this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                    this.InitialScore();
                }
            }

            db.DBClose(); // 关闭数据连接
        }

        /// <summary>
        /// The button 4_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button4_Click(object sender, EventArgs e)
        {
            this.DeleteScore(
                this.dataGridView1.SelectedRows[0].Cells["stuid"].Value.ToString(),
                this.dataGridView1.SelectedRows[0].Cells["numofcourse"].Value.ToString());
        }

        /// <summary>
        /// The main form_ resize.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void MainForm_Resize(object sender, EventArgs e)
        {
            this.autoAdaptSize?.FormSizeChanged();
        }

        /// <summary>
        /// The button 1_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button1_Click(object sender, EventArgs e)
        {
            // dataGridView1.Top = 25;
            // dataGridView1.Left = 30;
            // dataGridView1.Height = 350;
            // dataGridView1.Rows.Clear();//初始化
            this.dataGridView1.Visible = true;
            var db = new LinkSqLiteDB();
            var ds = new DataSet();
            if (db.DBConn())
            {
                // string xx = LoginForm.StudentId;

                // string sql = "select * from score_info where stuid='" + textBox1.Text + "' and stuname ='" + textBox3.Text + "'";
                var sql = "select * from score_info where stuid=" + LoginForm.StudentId;
                ds = db.GetScoreDs(sql);
                this.dataGridView1.DataSource = ds.Tables[0];
                this.dataGridView1.Update();

                // db.GetScoreData("select * from score_info where stuid='" + textBox1.Text + " and stuname =" + textBox3.Text +"'"); //执行sql
                // for (int i = 0; i < ScoreInfo.id.Count; i++)    //循环将数据实体类的数据存放到dataGridView中
                // {
                // int index = this.dataGridView1.Rows.Add();
                // this.dataGridView1.Rows[index].Cells[0].Value = ScoreInfo.id[i];        //填充
                // this.dataGridView1.Rows[index].Cells[1].Value = ScoreInfo.stuid[i];   
                // this.dataGridView1.Rows[index].Cells[2].Value = ScoreInfo.semester[i];    
                // this.dataGridView1.Rows[index].Cells[3].Value = ScoreInfo.numofcourse[i];   
                // this.dataGridView1.Rows[index].Cells[4].Value = ScoreInfo.nameofcourse[i];   
                // this.dataGridView1.Rows[index].Cells[5].Value = ScoreInfo.teacher[i];
                // this.dataGridView1.Rows[index].Cells[6].Value = ScoreInfo.credit[i];
                // this.dataGridView1.Rows[index].Cells[7].Value = ScoreInfo.grade[i];
                // this.dataGridView1.Rows[index].Cells[8].Value = ScoreInfo.stuname[i];
                // }
            }

            db.DBClose();

            if (ds.Tables[0].Rows.Count <= 0)
            {
                return;
            }

            // 获取学分
            var dt = ds.Tables[0];
            double jd = 0;

            // int sum_score = 0;
            double sumCredit = 0;
            double sumSc = 0; // 学分绩

            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var score = Convert.ToInt32(dt.Rows[i]["grade"].ToString());
                var credit = Convert.ToInt32(dt.Rows[i]["credit"].ToString());
                   
                if (score >= 90 && score <= 100)
                {
                    jd = 4;
                }

                if (score >= 85 && score <= 89)
                {
                    jd = 3.7;
                }

                if (score >= 82 && score <= 84)
                {
                    jd = 3.3;
                }

                if (score >= 78 && score <= 81)
                {
                    jd = 3.0;
                }

                if (score >= 75 && score <= 77)
                {
                    jd = 2.7;
                }

                if (score >= 72 && score <= 74)
                {
                    jd = 2.3;
                }

                if (score >= 68 && score <= 71)
                {
                    jd = 2.0;
                }

                if (score >= 64 && score <= 67)
                {
                    jd = 1.5;
                }

                if (score >= 60 && score <= 63)
                {
                    jd = 1.0;
                }

                if (score < 60)
                {
                    jd = 0;
                }

                sumSc += credit * jd;
                sumCredit += credit;
            }

            this.label2.Text = @"平均学分绩点为：" + Math.Round(sumSc / sumCredit, 2);

            // MessageBox.Show(sum_credit.ToString());
        }

        /// <summary>
        /// The Main_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void 主菜单_Load(object sender, EventArgs e)
        {
            // dataGridView1.Visible = false;//隐藏
            this.InitialScore();
       
            // button1.Enabled = false;//查询按钮暂时不可以点击
        }

        // 退出

        /// <summary>
        /// The button 2_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button2_Click(object sender, EventArgs e)
        {
            
            this.Hide();
        }

        // 添加

        /// <summary>
        /// The button 3_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button3_Click(object sender, EventArgs e)
        {
            var tj = new AddForm(LoginForm.StudentId, this);

            // tj.txt_stuid = textBox1.Text;
            // tj.txt_stuid = LoginForm.StudentId;
            // tj.txt_stuname = textBox3.Text;
            tj.Show();
        }
    }
}
